import { Department, DepartmentTreeInfo, User } from "@/type/departmentManage";
import { BaseAuthApi, LazyTableService } from "@/hooks/useLazyLable";
import axios, { Response, CancelToken, authGuard } from "./axios";
import factorySingleClass from "./SingleClass";

interface DepartmentManageService extends BaseAuthApi {
    SAVE: string;
    DELETEDAPARTMENT: string;
    UPDATE: string;

    SAVEUSER: string;
    UPDATEUSER: string;

    BATCHDEPARMENT: string;
    BATCHUSER: string;
}

let cancel: any;
class DepartmentManageService extends LazyTableService {
    getTree(orgId: any) {
        return axios.get<Response<DepartmentTreeInfo[]>>(
            `/usersys-auth/department/org-tree/${orgId}`,
        );
    }
    getDepartmentTree(orgId: any) {
        return axios.get<Response<DepartmentTreeInfo[]>>(
            `/usersys-auth/department/tree/${orgId}`,
        );
    }
    @authGuard
    save(obj: Department) {
        return axios.post<any>(`/usersys-auth/department/save`, obj);
    }
    @authGuard
    update(obj: Department) {
        return axios.put<any>(`/usersys-auth/department/update`, obj);
    }
    @authGuard
    getList(obj: any = {}) {
        cancel && cancel();
        return axios.post(`/usersys-auth/department/get-users`, obj, {
            cancelToken: new CancelToken((c) => (cancel = c)),
        });
    }
    @authGuard
    remove(id: string) {
        return axios.delete<any>(`/usersys-auth/user/delete?userId=${id}`);
    }
    getDepartmentById(id: string) {
        return axios.get<Response<Department>>(
            `/usersys-auth/department/get/${id}`,
        );
    }
    @authGuard
    deleteDapartment(id: string) {
        return axios.delete<any>(`/usersys-auth/department/delete/${id}`);
    }
    @authGuard
    saveUser(obj: User) {
        return axios.post<any>(`/usersys-auth/user/save-org-user`, obj);
    }
    getUserDetail(id: string) {
        return axios.get<Response<User>>(
            `/usersys-auth/user/get-user-detail/${id}`,
        );
    }
    @authGuard
    updateUser(obj: User) {
        return axios.post<any>(`/usersys-auth/user/update-org-user`, obj);
    }
    enable(id: number, orgId: number, state: number) {
        return axios.put<any>(
            `/usersys-auth/user/state?userId=${id}&state=${state}`,
        );
    }
    disable(id: number, orgId: number, state: number) {
        return axios.put<any>(
            `/usersys-auth/user/state?userId=${id}&state=${state}`,
        );
    }
    @authGuard
    batchDeparment() {
        return axios.post(`/usersys-auth/department/import`);
    }
    @authGuard
    batchUser() {
        return axios.post(`/usersys-auth/user/users`);
    }
}

export default factorySingleClass(DepartmentManageService);
